package com.atguigu.yygh.Controller.api;

import com.atguigu.yygh.common.MD5;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.service.DepartmentService;
import com.atguigu.yygh.service.HospitalService;
import com.atguigu.yygh.service.HospitalSetService;
import com.atguigu.yygh.service.ScheduleService;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @description: 医院系统调用我们的hosp微服务，将数据从医院端保存到Mongodb中
 * @author: 化一帆
 * @date: 2022/11/25 16:04
 * @version: v1.0
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * @description: 上传医院的接口
     * 得到医院系统通过post提交过来的数据【post提交过来的数据】
     * 把数据传到方法中，最终加入带Mongodb数据库中
     * @author 化一帆
     * @date 2022/11/25 16:11
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //1.获取从医院系统传递过来的医院的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //获取从医院系统中传过来的医院的签名[sign 是经过MD5加密后的签名]
        String hospSign = (String) ParamMap.get("sign");

        //根据从医院系统中传递过来的医院的编码hospCode，查询预约平台的数据库中对应的医院的签名【未加密】
        String hospCode = (String) ParamMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hospCode);

        //把数据库查出来的数据进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(20001, "签名校验失败");
        }

        //2.调用service层方法，将数据上传到Mongodb

        hospitalService.save(ParamMap);//save既可以新增，也可以做修改
        return Result.ok();
    }

    /**
     * @description: 根据医院编码，查询医院的接口
     * @author 化一帆
     * @date 2022/11/28 8:46
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //1.获取从医院系统传递过来的医院的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);//数组转换为对象

        //2.获取医院的编号
        String hashcode = (String) ParamMap.get("hoscode");//将对象转换为字符串

        //3.判断签名是否一致

        //获取从医院系统中传过来的医院的签名[sign 是经过MD5加密后的签名]
        String hospSign = (String) ParamMap.get("sign");

        //根据从医院系统中传递过来的医院的编码hoscode，查询预约平台的数据库中对应的医院的签名
        String hospCode = (String) ParamMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hospCode);

        //把数据库查出来的数据进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(20001, "签名校验失败");
        }

        //4.调用Service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hashcode);
        return Result.ok(hospital);
    }


    /**
     * @description: 上传科室的接口
     * @author 化一帆
     * @date 2022/11/28 10:19
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //2.获取医院的编号
        String hashcode = (String) ParamMap.get("hoscode");

        //3.判断签名是否一致

        //获取从医院系统中传过来的医院的签名[sign 是经过MD5加密后的签名]
        String hospSign = (String) ParamMap.get("sign");

        //根据从医院系统中传递过来的医院的编码hoscode，查询预约平台的数据库中对应的医院的签名
        String hospCode = (String) ParamMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hospCode);

        //把数据库查出来的数据进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(20001, "签名校验失败");
        }

        //4.调用departmentService中的方法
        departmentService.saveDepartment(ParamMap);
        return Result.ok();
    }

    /**
     * @description: 查询科室的接口
     * 分页查询
     * page 当前页
     * limit 每页的记录数
     * departmentQueryVo 查询条件
     * @author 化一帆
     * @date 2022/11/28 10:57
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //TODO 1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 2.获取医院的编号
        String hoscode = (String) ParamMap.get("hoscode");

        //判断page是否存在，如果存在就使用[转换成int类型]，如果不存在赋默认值1
        int page = StringUtils.isEmpty(ParamMap.get("page")) ? 1 : Integer.parseInt((String) ParamMap.get("page"));
        int limit = StringUtils.isEmpty(ParamMap.get("limit")) ? 1 : Integer.parseInt((String) ParamMap.get("limit"));

        //TODO 3.将条件封装到vo对象
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * @description: 删除科室接口
     * @author 化一帆
     * @date 2022/11/28 17:09
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //TODO 1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 2.医院编号和科室编号
        String hoscode = (String) ParamMap.get("hoscode");
        String depcode = (String) ParamMap.get("depcode");

        //TODO 3.签名的校验

        //TODO 4.调用service层的方法
        departmentService.removeDepartment(hoscode, depcode);

        return Result.ok();
    }


    /**
     * @description: 上传排班的接口
     * @author 化一帆
     * @date 2022/11/28 17:56
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //TODO 1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 2.签名的校验

        //TODO 3.调用Service层的方法
        scheduleService.save(ParamMap);
        return Result.ok();
    }

    /**
     * @description: 查询排班的接口
     * @author 化一帆
     * @date 2022/11/28 18:17
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //TODO 1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 2.获取医院编号和科室编号
        String hosCode = (String) ParamMap.get("hosCode");
        String depCode = (String) ParamMap.get("depCode");

        //TODO 3.判断page和limit是否存在，如果存在就使用[转换成int类型]，如果不存在赋默认值1
        int page = StringUtils.isEmpty(ParamMap.get("page")) ? 1 : Integer.parseInt((String) ParamMap.get("page"));
        int limit = StringUtils.isEmpty(ParamMap.get("limit")) ? 1 : Integer.parseInt((String) ParamMap.get("limit"));

        //TODO 4.将查询条件封装到VO对象中去
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hosCode);
        scheduleQueryVo.setHoscode(depCode);

        //TODO 5.调用Service层的方法实现分页查询
        Page<Schedule> pageModel= (Page<Schedule>) scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * @description: 删除排班的接口
     * @author 化一帆
     * @date 2022/11/28 19:12
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //TODO 1.获取从医院系统传递过来的科室的信息【医院系统将数据封装为了map】
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> ParamMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 2.获取医院编号和排班编号
        String hosCode = (String) ParamMap.get("hosCode");
        String hosScheduleId = (String) ParamMap.get("hosScheduleId");

        //TODO 3.调用Service层的方法
        scheduleService.removeSchedule(hosCode,hosScheduleId);

        //TODO 4.返回结果
        return Result.ok();
    }
}
